package com.ly.admin.controller;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.ass.nacos.constant.NacosConfigFileType;
import com.ass.nacos.service.NacosApiService;
import com.common.core.domain.AjaxResult;
import com.common.core.web.controller.BaseController;
import com.common.fileupload.utils.FileUtil;
import com.ly.admin.dto.input.InstanceInput;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/serverCtrl")
@RequiredArgsConstructor
@Api(tags = "服务监控中心")
public class ServerController extends BaseController {

    private final NacosApiService nacosApiService;


    @GetMapping("/getConfigValue")
    @ApiOperation(value = "获取nacos 对应组的配置 组里面的配置文件内容",notes = "李勇")
    public AjaxResult getConfigValue(@ApiParam(value = "nacos 里面的文件id", required = true) @NotBlank String dataId,
                                    @ApiParam(value = "nacos 组 id", required = true) @NotBlank String group,
                                    @ApiParam(value = "网络超时时间 默认3000ms") Long timeoutMs){
        String configValue = nacosApiService.getConfigValue(dataId, group, timeoutMs);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("configValue",configValue);
        return ajax;
    }




    @PostMapping(value = "/publishConfig")
    @ApiOperation(value = "nacos配置文件上传",notes = "李勇")
    public AjaxResult publishConfig(@ApiParam(value = "文件", required = true) @NotNull MultipartFile file,
                                    @ApiParam(value = "nacos 组 id", required = true) @NotBlank String group){
        if(file==null){
            return AjaxResult.error("文件不能为空");
        }
        String dataId = file.getOriginalFilename();
        String suffix = dataId.substring(dataId.indexOf(".")+1);
        boolean suffixFlag = Arrays.stream(NacosConfigFileType.values()).anyMatch(item-> Objects.equals(item.getValue(),suffix));
        if(!suffixFlag){
            return AjaxResult.error("文件类型仅支持：properties、xml、json、text、html、yaml、unset");
        }
        boolean b = nacosApiService.publishConfig(dataId, group, FileUtil.readFileContent(file), suffix);
        return toAjax(b);
    }


    @DeleteMapping(value = "/removeConfig")
    @ApiOperation(value = "nacos配置文件删除",notes = "李勇")
    public AjaxResult removeConfig(  @ApiParam(value = "nacos 里面的文件id", required = true) @Validated @NotBlank String dataId,
                                     @ApiParam(value = "nacos 组 id", required = true) @Validated @NotBlank String group){
        return toAjax(nacosApiService.removeConfig(dataId,group));
    }

    //对配置文件有个编辑


    @PostMapping(value = "/registerInstance")
    @ApiOperation(value = "注册实例 向某个服务里面",notes = "李勇")
    public AjaxResult registerInstance(@ApiParam @RequestBody @Validated InstanceInput input) {
        return toAjax(nacosApiService.registerInstance(input.getServiceName(),input.getInstance()));
    }



    @PostMapping(value = "/deregisterInstance")
    @ApiOperation(value = "注销实例 向某个服务里面",notes = "李勇")
    public AjaxResult deregisterInstance(@ApiParam(value = "服务名", required = true) @Validated @NotBlank String serviceName,
                                         @ApiParam(value = "ip", required = true) @Validated @NotBlank String ip,
                                         @ApiParam(value = "port", required = true) @Validated @NotBlank int port) {
        return toAjax(nacosApiService.deregisterInstance(serviceName,ip,port));
    }

    @GetMapping(value = "/getAllInstances")
    @ApiOperation(value = "获得某个服务下全部实例",notes = "李勇")
    public AjaxResult getAllInstances(@ApiParam(value = "服务名", required = true) String serviceName){
        return AjaxResult.success().put("allInstances",nacosApiService.getAllInstances(serviceName));
    }

    @GetMapping("/getInstances")
    @ApiOperation(value = "获取健康或者不健康的实例",notes = "李勇")
    public AjaxResult getInstances(@ApiParam(value = "服务名", required = true)  String serviceName,
                           @ApiParam(value = "健康状态", required = true)  boolean healthy) {
        List<Instance> instances = nacosApiService.selectInstances(serviceName, healthy);
        return AjaxResult.success().put("instances",instances);
    }


    @GetMapping("/getOneHealthyInstance")
    @ApiOperation(value = "获取一个健康的实例",notes = "李勇")
    public AjaxResult getOneHealthyInstance(@ApiParam(value = "服务名", required = true)  String serviceName) {
        Instance instance = nacosApiService.selectOneHealthyInstance(serviceName);
        return AjaxResult.success().put("instance",instance);
    }




}
